var app = getApp();
Page({
  data: {
    orderId: '',
    oldReceivedAmount: '',
    oldAmount:'',
    roomPrice: '',
    cancelReason: '',
    disableBtn: true,
    cancelReasonNum: 0,
    flag:false
  },
  onLoad: function (options) {
    this.setData({
      orderId: options.orderId,
      oldReceivedAmount: options.oldReceivedAmount,
      oldAmount: options.oldAmount,
      roomPrice: options.oldReceivedAmount
    })
  },
  getPrice: function (event) {
    var _this = this;
    _this.setData({
      roomPrice: event.detail.value
    })
    if (_this.data.roomPrice >= _this.data.oldAmount) {
      _this.setData({
        roomPrice: _this.data.oldAmount
      })
    }
    _this.sureAble();
  },
  //获取取消原因
  cancelReason: function (event) {
    this.setData({
      cancelReason: event.detail.value,
      cancelReasonNum: event.detail.value.length
    })
    this.sureAble();
  },
  //确定按钮高亮判断
  sureAble: function () {
    var _this = this;
    if (_this.data.cancelReason.length > 0 && _this.data.roomPrice > 0) {
      this.setData({
        disableBtn: false
      })
    } else {
      this.setData({
        disableBtn: true
      })
    }

  },
  //点击确定按钮
  modifyOrder: function () {
    var _this = this;
    if (_this.data.flag) {
      return;
    } else {
      _this.setData({
        flag: true
      })
      app.request({
        url: app.globalData.apiUrl +'/mobileOfficing/api/roomReservation/updateOrder',
        method: 'POST',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'sessionId': wx.getStorageSync(app.globalData.sessionId)
        },
        data: {
          orderId: _this.data.orderId,
          updateCause: _this.data.cancelReason,
          oldReceivedAmount: _this.data.oldReceivedAmount,
          toUpdateAmount: _this.data.roomPrice
        },
        success: function (res) {
          //console.log(res)
          if (res.data.result) {
            wx.showToast({
              title: '修改成功',
              icon: 'success',
              duration: 1000
            })
            setTimeout(function () {
              wx.redirectTo({
                url: '../details/details?orderId=' + _this.data.orderId
              })
            }, 2500)

          } else {
            _this.setData({
              flag: false
            })
            var msg = res.data.msg
            wx.showModal({
              title: '提示',
              content: msg,
              showCancel: false,
              confirmText: '知道了',
              success: function (res) {
                if (res.confirm) {
                  if (msg.indexOf('金额') >= 0 || msg.indexOf('请求参数') >= 0) {
                    console.log('用户点击确定')
                  } else {
                    wx.redirectTo({
                      url: '../details/details?orderId=' + _this.data.orderId
                    })
                  }
                }
              }
            })
          }
        }
      })
    }
  },
  //点击返回详情页
  goBackDetails: function () {
    var _this = this;
    wx.redirectTo({
      url: '../details/details?orderId=' + _this.data.orderId
    })
  }



})